import pygame, sys,win32api,win32con,win32gui
import re
import sys
import os
import itertools
import math
from tkinter import *
from pygame.locals import *
from socket import *
import threading
import json
from _thread import *
import copy

WINDOWWIDTH = 1400
WINDOWHEIGHT = 800
windows1=pygame.display.set_mode((WINDOWWIDTH,WINDOWHEIGHT))
pygame.display.set_caption('AI版')
windows1.fill((255,228,181))
g1 = 50
g2 = 64.2857
r = 9
pygame.init()
nuMber = pygame.font.Font(None, 30)
jianti = pygame.font.Font('C:\Windows\Fonts\STXINGKA.TTF', 25)
cuti = pygame.font.Font('C:\Windows\Fonts\ARLRDBD.TTF', 25)
cuti2 = pygame.font.Font('C:\Windows\Fonts\STHUPO.TTF', 50)
client_sock = socket(AF_INET,SOCK_STREAM)
client_sock.connect(('127.0.0.1',50005))

class chessPos:

    def __init__(self,x,y,tf,num,index,basic):
        self.x = x
        self.y = y 
        self.tf = tf 
        self.num = num
        self.index = index
        self.basic = basic
p0 = chessPos(400,400,1,0,0,0)
p1 = chessPos(400+1*g2,400-1*g1,1,9,1,9)
p2 = chessPos(400+1*g2,400+1*g1,1,8,2,8)
p3 = chessPos(400+2*g2,400-2*g1,1,5,3,5)
p4 = chessPos(400+2*g2,400+0*g1,1,6,4,6)
p5 = chessPos(400+2*g2,400+2*g1,1,7,5,7)
p6 = chessPos(400+3*g2,400-3*g1,1,4,6,4)
p7 = chessPos(400+3*g2,400-1*g1,1,2,7,2)
p8 = chessPos(400+3*g2,400+1*g1,1,3,8,3)
p9 = chessPos(400+3*g2,400+3*g1,1,1,9,1)

p10 = chessPos(400+4*g2,400-4*g1,0,-1,10,-1) #第十一个棋盘点
p11 = chessPos(400+4*g2,400-2*g1,0,-1,11,-1)
p12 = chessPos(400+4*g2,400+0*g1,0,-1,12,-1)
p13 = chessPos(400+4*g2,400+2*g1,0,-1,13,-1)
p14 = chessPos(400+4*g2,400+4*g1,0,-1,14,-1)

p15 = chessPos(400+5*g2,400-5*g1,0,-1,15,-1)
p16 = chessPos(400+5*g2,400-3*g1,0,-1,16,-1)
p17 = chessPos(400+5*g2,400-1*g1,0,-1,17,-1)
p18 = chessPos(400+5*g2,400+1*g1,0,-1,18,-1)
p19 = chessPos(400+5*g2,400+3*g1,0,-1,19,-1)
p20 = chessPos(400+5*g2,400+5*g1,0,-1,20,-1)

p21 = chessPos(400+6*g2,400-6*g1,0,-1,21,-1)
p22 = chessPos(400+6*g2,400-4*g1,0,-1,22,-1)
p23 = chessPos(400+6*g2,400-2*g1,0,-1,23,-1)
p24 = chessPos(400+6*g2,400+0*g1,0,-1,24,-1)
p25 = chessPos(400+6*g2,400+2*g1,0,-1,25,-1)
p26 = chessPos(400+6*g2,400+4*g1,0,-1,26,-1)
p27 = chessPos(400+6*g2,400+6*g1,0,-1,27,-1)

p28 = chessPos(400+7*g2,400-7*g1,0,-1,28,-1)
p29 = chessPos(400+7*g2,400-5*g1,0,-1,29,-1)
p30 = chessPos(400+7*g2,400-3*g1,0,-1,30,-1)
p31 = chessPos(400+7*g2,400-1*g1,0,-1,31,-1)
p32 = chessPos(400+7*g2,400+1*g1,0,-1,32,-1)
p33 = chessPos(400+7*g2,400+3*g1,0,-1,33,-1)
p34 = chessPos(400+7*g2,400+5*g1,0,-1,34,-1)
p35 = chessPos(400+7*g2,400+7*g1,0,-1,35,-1)

p36 = chessPos(400+8*g2,400-6*g1,0,-1,36,-1)
p37 = chessPos(400+8*g2,400-4*g1,0,-1,37,-1)
p38 = chessPos(400+8*g2,400-2*g1,0,-1,38,-1)
p39 = chessPos(400+8*g2,400+0*g1,0,-1,39,-1)
p40 = chessPos(400+8*g2,400+2*g1,0,-1,40,-1)
p41 = chessPos(400+8*g2,400+4*g1,0,-1,41,-1)
p42 = chessPos(400+8*g2,400+6*g1,0,-1,42,-1)

p43 = chessPos(400+9*g2,400-5*g1,0,-1,43,-1)
p44 = chessPos(400+9*g2,400-3*g1,0,-1,44,-1)
p45 = chessPos(400+9*g2,400-1*g1,0,-1,45,-1)
p46 = chessPos(400+9*g2,400+1*g1,0,-1,46,-1)
p47 = chessPos(400+9*g2,400+3*g1,0,-1,47,-1)
p48 = chessPos(400+9*g2,400+5*g1,0,-1,48,-1)

p49 = chessPos(400+10*g2,400-4*g1,0,-1,49,-1) 
p50 = chessPos(400+10*g2,400-2*g1,0,-1,50,-1)
p51 = chessPos(400+10*g2,400+0*g1,0,-1,51,-1)
p52 = chessPos(400+10*g2,400+2*g1,0,-1,52,-1)
p53 = chessPos(400+10*g2,400+4*g1,0,-1,53,-1) #最后一个无棋子棋盘点

#蓝方十个棋子
p54 = chessPos(400+11*g2,400-3*g1,1,11,54,11) #为了区分红蓝棋子将蓝色方对应编号棋子+10
p55 = chessPos(400+11*g2,400-1*g1,1,13,55,13)
p56 = chessPos(400+11*g2,400+1*g1,1,12,56,12)
p57 = chessPos(400+11*g2,400+3*g1,1,14,57,14)
p58 = chessPos(400+12*g2,400-2*g1,1,17,58,17)
p59 = chessPos(400+12*g2,400+0*g1,1,16,59,16)
p60 = chessPos(400+12*g2,400+2*g1,1,15,60,15)
p61 = chessPos(400+13*g2,400-1*g1,1,18,61,18)
p62 = chessPos(400+13*g2,400+1*g1,1,19,62,19)
p63 = chessPos(400+14*g2,400+0*g1,1,10,63,10)

chessPoint = {0:p0,1:p1,2:p2,3:p3,4:p4,5:p5,6:p6,7:p7,8:p8,9:p9,
              10:p10,11:p11,12:p12,13:p13,14:p14,15:p15,16:p16,17:p17,18:p18,19:p19,
              20:p20,21:p21,22:p22,23:p23,24:p24,25:p25,26:p26,27:p27,28:p28,29:p29,
              30:p30,31:p31,32:p32,33:p33,34:p34,35:p35,36:p36,37:p37,38:p38,39:p39,
              40:p40,41:p41,42:p42,43:p43,44:p44,45:p45,46:p46,47:p47,48:p48,49:p49,
              50:p50,51:p51,52:p52,53:p53,54:p54,55:p55,56:p56,57:p57,58:p58,59:p59,
              60:p60,61:p61,62:p62,63:p63}

xiangdui_chessPoint = { 0:[0,7],1:[1,6],2:[1,8],3:[2,5],4:[2,7],5:[2,9],6:[3,4],7:[3,6],8:[3,8],9:[3,10],
                        10:[4,3],11:[4,5],12:[4,7],13:[4,9],14:[4,11],
                        15:[5,2],16:[5,4],17:[5,6],18:[5,8],19:[5,10],20:[5,12],
                        21:[6,1],22:[6,3],23:[6,5],24:[6,7],25:[6,9],26:[6,11],27:[6,13],
                        28:[7,0],29:[7,2],30:[7,4],31:[7,6],32:[7,8],33:[7,10],34:[7,12],35:[7,14],
                        36:[8,1],37:[8,3],38:[8,5],39:[8,7],40:[8,9],41:[8,11],42:[8,13],
                        43:[9,2],44:[9,4],45:[9,6],46:[9,8],47:[9,10],48:[9,12],
                        49:[10,3],50:[10,5],51:[10,7],52:[10,9],53:[10,11],
                        54:[11,4],55:[11,6],56:[11,8],57:[11,10],58:[12,5],59:[12,7],60:[12,9],61:[13,6],62:[13,8],63:[14,7]}

xiangdui_suoyin_chessPoint = {(0,7):0,(1,6):1,(1,8):2,(2,5):3,(2,7):4,(2,9):5,(3,4):6,(3,6):7,(3,8):8,(3,10):9,
                                (4,3):10,(4,5):11,(4,7):12,(4,9):13,(4,11):14,
                                (5,2):15,(5,4):16,(5,6):17,(5,8):18,(5,10):19,(5,12):20,
                                (6,1):21,(6,3):22,(6,5):23,(6,7):24,(6,9):25,(6,11):26,(6,13):27,
                                (7,0):28,(7,2):29,(7,4):30,(7,6):31,(7,8):32,(7,10):33,(7,12):34,(7,14):35,
                                (8,1):36,(8,3):37,(8,5):38,(8,7):39,(8,9):40,(8,11):41,(8,13):42,
                                (9,2):43,(9,4):44,(9,6):45,(9,8):46,(9,10):47,(9,12):48,
                                (10,3):49,(10,5):50,(10,7):51,(10,9):52,(10,11):53,
                                (11,4):54,(11,6):55,(11,8):56,(11,10):57,(12,5):58,(12,7):59,(12,9):60,(13,6):61,(13,8):62,(14,7):63 }
def restart_program():
    python = sys.executable
    os.execl(python, python, * sys.argv)

def huaqipan():
    for i in range(0,64):
        if chessPoint[i].tf == 0:
            if chessPoint[i].num == -1:
                if 9 < chessPoint[i].index < 54:
                    pygame.draw.circle(windows1,(255,255,255),(chessPoint[i].x,chessPoint[i].y),19,0)
                elif -1 < chessPoint[i].index < 10:
                    pygame.draw.circle(windows1,(180,180,255),(chessPoint[i].x,chessPoint[i].y),19,0)
                elif 53 < chessPoint[i].index < 64:
                    pygame.draw.circle(windows1,(255,180,180),(chessPoint[i].x,chessPoint[i].y),19,0)
        else:
            if 0 <= chessPoint[i].index < 10:
                if 9 < chessPoint[i].num < 20:
                     pygame.draw.circle(windows1,(255,0,0),(chessPoint[i].x,chessPoint[i].y),19,0)
                elif -1 < chessPoint[i].num < 10:
                     pygame.draw.circle(windows1,(0,0,255),(chessPoint[i].x,chessPoint[i].y),19,0)
            elif 53 < chessPoint[i].index < 64:
                if 9 < chessPoint[i].num < 20:
                     pygame.draw.circle(windows1,(255,0,0),(chessPoint[i].x,chessPoint[i].y),19,0)
                elif -1 < chessPoint[i].num < 10:
                     pygame.draw.circle(windows1,(0,0,255),(chessPoint[i].x,chessPoint[i].y),19,0)
            else:
                if -1 < chessPoint[i].num < 10:
                    pygame.draw.circle(windows1,(0,0,255),(chessPoint[i].x,chessPoint[i].y),19,0)
                elif 9 < chessPoint[i].num < 20:
                    pygame.draw.circle(windows1,(255,0,0),(chessPoint[i].x,chessPoint[i].y),19,0)
def make_Pan():

    pygame.draw.line(windows1,(0,0,0),(400,400),(850,50),1)
    pygame.draw.line(windows1,(0,0,0),(850,50),(1300,400),0)
    pygame.draw.line(windows1,(0,0,0),(1300,400),(850,750),1)
    pygame.draw.line(windows1,(0,0,0),(850,750),(400,400),1)
    for j in range(1,7):
        pygame.draw.line(windows1,(0,0,0),(1300-g2*j,400-g1*j),(1300-g2*j,400+g1*j),1)
        pygame.draw.line(windows1,(0,0,0),(1300-g2*j,400-g1*j),(850-g2*j,750-g1*j),1)  
    for i in range(1,8):
        pygame.draw.line(windows1,(0,0,0),(400+g2*i,400-g1*i),(400+g2*i,400+g1*i),1)
        pygame.draw.line(windows1,(0,0,0),(400+g2*i,400-g1*i),(850+g2*i,750-g1*i),1)
    for i in range(1,8):
        for ii in range(0,8):
                pygame.draw.circle(windows1,(255,255,255),(400+g2*(i-1)+g2*ii,400-g1*(i-1)+g1*ii),18,0)
                pygame.draw.circle(windows1,(255,255,255),(850+g2*ii,50+g1*ii),18,0)
    for i in range(0,4):
        pygame.draw.circle(windows1,(255,0,0),(400+g2*i,400-g1*i),19,0)
        pygame.draw.circle(windows1,(255,0,0),(400+g2*i,400+g1*i),19,0)
        pygame.draw.circle(windows1,(0,0,255),(1300-g2*i,400-g1*i),19,0)
        pygame.draw.circle(windows1,(0,0,255),(1300-g2*i,400+g1*i),19,0)
    pygame.draw.circle(windows1,(255,0,0),(400+2*g2,400),19,0)
    pygame.draw.circle(windows1,(255,0,0),(400+3*g2,400-g1),19,0)
    pygame.draw.circle(windows1,(255,0,0),(400+3*g2,400+g1),19,0)
    pygame.draw.circle(windows1,(0,0,255),(1300-g2*2,400),19,0)
    pygame.draw.circle(windows1,(0,0,255),(1300-g2*3,400+g1),19,0)
    pygame.draw.circle(windows1,(0,0,255),(1300-g2*3,400-g1),19,0)
def huashuzi():
    nuMber = pygame.font.Font(None, 28)
    xx = 5
    windows1.blit(nuMber.render("0", True, (0,0,0)), (400-xx, 400-xx))
    windows1.blit(nuMber.render("8", True, (0,0,0)), (400+g2-xx, 400+g1-xx))
    windows1.blit(nuMber.render("9", True, (0,0,0)), (400+g2-xx, 400-g1-xx))
    windows1.blit(nuMber.render("5", True, (0,0,0)), (400+2*g2-xx, 400-2*g1-xx))
    windows1.blit(nuMber.render("6", True, (0,0,0)), (400+2*g2-xx, 400-xx))
    windows1.blit(nuMber.render("7", True, (0,0,0)), (400+2*g2-xx, 400+2*g1-xx))
    windows1.blit(nuMber.render("4", True, (0,0,0)), (400+3*g2-xx, 400-3*g1-xx))
    windows1.blit(nuMber.render("2", True, (0,0,0)), (400+3*g2-xx, 400-g1-xx))
    windows1.blit(nuMber.render("3", True, (0,0,0)), (400+3*g2-xx, 400+g1-xx))
    windows1.blit(nuMber.render("1", True, (0,0,0)), (400+3*g2-xx, 400+3*g1-xx))


    windows1.blit(nuMber.render("0", True, (0,0,0)), (400+14*g2-xx, 400-xx))
    windows1.blit(nuMber.render("8", True, (0,0,0)), (400+13*g2-xx, 400-g1-xx))
    windows1.blit(nuMber.render("9", True, (0,0,0)), (400+13*g2-xx, 400+g1-xx))
    windows1.blit(nuMber.render("5", True, (0,0,0)), (400+12*g2-xx, 400+2*g1-xx))
    windows1.blit(nuMber.render("6", True, (0,0,0)), (400+12*g2-xx, 400-xx))
    windows1.blit(nuMber.render("7", True, (0,0,0)), (400+12*g2-xx, 400-2*g1-xx))
    windows1.blit(nuMber.render("4", True, (0,0,0)), (400+11*g2-xx, 400+3*g1-xx))
    windows1.blit(nuMber.render("2", True, (0,0,0)), (400+11*g2-xx, 400+g1-xx))
    windows1.blit(nuMber.render("3", True, (0,0,0)), (400+11*g2-xx, 400-g1-xx))
    windows1.blit(nuMber.render("1", True, (0,0,0)), (400+11*g2-xx, 400-3*g1-xx)) #蓝色方大本营

    for j in range(0,64):
        for i in range(0,10):
            if i == chessPoint[j].num:
                pygame.draw.circle(windows1,(255,255,255),(chessPoint[j].x,chessPoint[j].y),15,0)
                windows1.blit(nuMber.render(str(i),True,(0,0,0)),(chessPoint[j].x-xx,chessPoint[j].y-xx))
        for ii in range(10,20):
            if ii == chessPoint[j].num:
                pygame.draw.circle(windows1,(255,255,255),(chessPoint[j].x,chessPoint[j].y),15,0)
                windows1.blit(nuMber.render(str(ii-10),True,(0,0,0)),(chessPoint[j].x-xx,chessPoint[j].y-xx))

def make_others():
    pygame.draw.rect(windows1, (0,191,255), [50, 50, 110, 30], 0)
    pygame.draw.rect(windows1, (0,191,255), [180, 50, 110, 30], 0)
    windows1.blit(jianti.render("退出游戏", True, (0,0,0)), (55,55))
    windows1.blit(jianti.render("重新开始", True, (0,0,0)), (185,55))
    pygame.draw.rect(windows1, (0,0,255), [100, 120, 120, 40], 3)
    pygame.draw.rect(windows1, (0,0,255), [100, 220, 120, 40], 3)
    windows1.blit(jianti.render("蓝方得分", True, (0,0,0)), (105,125))
    windows1.blit(jianti.render("红方得分", True, (0,0,0)), (105,225))
    # pygame.draw.rect(windows1, (255,255,255), [30, 450, 110, 50], 0)
    # windows1.blit(cuti2.render("悔棋", True, (0,0,0)), (30, 450))
    pygame.draw.rect(windows1, (255,255,255), [30, 520, 110, 50], 0)
    windows1.blit(cuti2.render("认输", True, (0,0,0)), (30, 520))
    pygame.draw.rect(windows1, (255,255,255), [30, 580, 110, 50], 0)
    windows1.blit(cuti2.render("叫停", True, (0,0,0)), (30, 580))


def renshu(x,y,k):
    if 30 < x < 140 and 520 < y < 570:
        if k % 2 != 0:
            root3=Tk()
            Label(root3,text='蓝方认输').pack()
            Label(root3,text='红方胜利').pack()
            root3.geometry("%dx%d+%d+%d" % (200, 100, (WINDOWWIDTH +50) / 2, (WINDOWHEIGHT +50) / 2))
            root3.mainloop()
            return 1
            restart_program()
            
        else:
            root3=Tk()
            Label(root3,text='红方认输').pack()
            Label(root3,text='蓝方胜利').pack()
            root3.geometry("%dx%d+%d+%d" % (200, 100, (WINDOWWIDTH +50) / 2, (WINDOWHEIGHT +50) / 2))
            root3.mainloop()
            return 1
            restart_program()
    else:
        return -1
def jiaoting(k):
    k1 = 1
    k2 = 1
    for i in range(0,10):
        if chessPoint[i].num <10:
            k1 = 0
    for j in range(54,64):
        if chessPoint[j].num <0 or 10 <= chessPoint[j].num <20:
            k2 = 0

    if k % 2 != 0 and k2 == 1  :
        if(defen()[1] > defen()[0]):

            root3=Tk()
            Label(root3,text='蓝方叫停成功').pack()
            Label(root3,text='蓝方胜利').pack()
            root3.geometry("%dx%d+%d+%d" % (200, 100, (WINDOWWIDTH +50) / 2, (WINDOWHEIGHT +50) / 2))
            root3.mainloop()
            return 1
        else:
            root3=Tk()
            Label(root3,text='蓝方叫停成功').pack()
            Label(root3,text='红方胜利').pack()
            root3.geometry("%dx%d+%d+%d" % (200, 100, (WINDOWWIDTH +50) / 2, (WINDOWHEIGHT +50) / 2))
            root3.mainloop()
        
            return 1
    elif k % 2 == 0 and k1 == 1 :
        if(defen()[1] > defen()[0]):

            root3=Tk()
            Label(root3,text='红方叫停成功').pack()
            Label(root3,text='蓝方胜利').pack()
            root3.geometry("%dx%d+%d+%d" % (200, 100, (WINDOWWIDTH +50) / 2, (WINDOWHEIGHT +50) / 2))
            root3.mainloop()
            return 1
        else:
            root3=Tk()
            Label(root3,text='红方叫停成功').pack()
            Label(root3,text='红方胜利').pack()
            root3.geometry("%dx%d+%d+%d" % (200, 100, (WINDOWWIDTH +50) / 2, (WINDOWHEIGHT +50) / 2))
            root3.mainloop()
            return 1
        
    else:
        root3=Tk()
        Label(root3,text='叫停失败').pack()
        root3.geometry("%dx%d+%d+%d" % (100, 100, (WINDOWWIDTH +50) / 2, (WINDOWHEIGHT +50) / 2))
        root3.mainloop()
        return -1       

jiaoting_flag = [0]

def jiaoting2(x,y,k):
    k1 = 1
    k2 = 1
    for i in range(0,10):
        if chessPoint[i].num <10:
            k1 = 0
    for j in range(54,64):
        if chessPoint[j].num <0 or 10 <= chessPoint[j].num <20:
            k2 = 0
    if 30 < x < 140 and 580 < y < 620:
        if k % 2 != 0 and k2 == 1  :
            if(defen()[1] > defen()[0]):

                jiaoting_flag[0] = 1
                root3=Tk()
                Label(root3,text='蓝方叫停成功').pack()
                Label(root3,text='蓝方胜利').pack()
                root3.geometry("%dx%d+%d+%d" % (200, 100, (WINDOWWIDTH +50) / 2, (WINDOWHEIGHT +50) / 2))
                root3.mainloop()

            else:
                jiaoting_flag[0] = 1
                root3=Tk()
                Label(root3,text='蓝方叫停成功').pack()
                Label(root3,text='红方胜利').pack()
                root3.geometry("%dx%d+%d+%d" % (200, 100, (WINDOWWIDTH +50) / 2, (WINDOWHEIGHT +50) / 2))
                root3.mainloop()
              

          
        elif k % 2 == 0 and k1 == 1 :
            if(defen()[1] > defen()[0]):
                jiaoting_flag[0] = 1
                root3=Tk()
                Label(root3,text='红方叫停成功').pack()
                Label(root3,text='蓝方胜利').pack()
                root3.geometry("%dx%d+%d+%d" % (200, 100, (WINDOWWIDTH +50) / 2, (WINDOWHEIGHT +50) / 2))
                root3.mainloop()
            else:
                jiaoting_flag[0] = 1
                root3=Tk()
                Label(root3,text='红方叫停成功').pack()
                Label(root3,text='红方胜利').pack()
                root3.geometry("%dx%d+%d+%d" % (200, 100, (WINDOWWIDTH +50) / 2, (WINDOWHEIGHT +50) / 2))
                root3.mainloop()
     
        else:
            root3=Tk()
            Label(root3,text='叫停失败').pack()
            root3.geometry("%dx%d+%d+%d" % (100, 100, (WINDOWWIDTH +50) / 2, (WINDOWHEIGHT +50) / 2))
            root3.mainloop()          

def make_defen():
    pygame.draw.rect(windows1, (0,0,255), [255, 105, 60, 60], 0)
    pygame.draw.rect(windows1, (255,0,0), [255, 205, 60, 60], 0)
    windows1.blit(cuti.render(str(defen()[1]), True, (0,0,0)), (260,108))
    windows1.blit(cuti.render(str(defen()[0]), True, (0,0,0)), (260,208))
#    pygame.draw.rect(windows1,(0,0,0),[p0.x-2*r,p0.y-2*r,4*r,4*r],3)
 #   pygame.draw.circle(windows1,(127,255,0),(chessPoint[2].x,chessPoint[2].y),18,6)
def tuichu(x,y):
    if (50<x<170 and 50<y<80):
        pygame.quit()
        sys.exit()
##选中
def xuanzhong(x,y,k,sd):
    if k < 0:
        for i in range(0,64):
            xx = chessPoint[i].x
            yy = chessPoint[i].y
            quyu = pygame.Rect(xx-2*r,yy-2*r,4*r,4*r)
            if quyu.collidepoint(x,y):
                return(int(i))
    elif k >= 0 and sd == 1:
        for i in range(0,64):
            if k%2 != 0:
                xx = chessPoint[i].x
                yy = chessPoint[i].y
                quyu = pygame.Rect(xx-2*r,yy-2*r,4*r,4*r)
                if -1 < chessPoint[i].num <10 or chessPoint[i].tf == 0:
                    if quyu.collidepoint(x,y):
                        return(int(i))
    elif k >= 0 and sd == 0:
        for i in range(0,64):
            if k%2 == 0:
                xx = chessPoint[i].x
                yy = chessPoint[i].y
                quyu = pygame.Rect(xx-2*r,yy-2*r,4*r,4*r)
                if 9 < chessPoint[i].num < 20 or chessPoint[i].tf == 0:
                    if quyu.collidepoint(x,y):
                        return(int(i))     
    return(None)


def can_move(before,after):
    if xianglin(before,after) and chessPoint[before].tf > 0 and chessPoint[after].tf == 0:
        return True
    return False


def can_jump(before,after):
    if zhongdian(before,after) and chessPoint[before].tf>0 and chessPoint[after].tf==0:
        return True
    return False

def yidong(before,after):
    chessPoint[before].tf = 0
    chessPoint[after].tf = 1
    chessPoint[after].num = chessPoint[before].num
    chessPoint[before].num = -1

def xianglin(before,after):#判断是否相邻
    xx = abs(chessPoint[before].x - chessPoint[after].x)
    yy = abs(chessPoint[before].y - chessPoint[after].y)
    xx1 = int(xx)
    yy1 = int(yy)
    if(xx1 == int(g2) and yy1 ==int(g1)):
        return True
    elif(xx1 == 0 and yy1 == int(2*g1)):
        return True
    else:
        return False

def zhixian(before,after):#判断是否在一条线上
    if round(abs(chessPoint[before].x-chessPoint[after].x)/g2,1) == round(abs(chessPoint[before].y-chessPoint[after].y)/g1,1):
        return True
    elif(chessPoint[before].x == chessPoint[after].x):
        return True
    else:
        return False  

def jiange(before,after):
    if zhixian(before,after) == True:
        if int(abs(chessPoint[before].x-chessPoint[after].x)) == int(2*g2) and int(abs(chessPoint[before].y-chessPoint[after].y)) == int(2*g1):
            return True
        elif int(abs(chessPoint[before].x-chessPoint[after].x)) == 0 and int(abs(chessPoint[before].y-chessPoint[after].y)) == int(4*g1):
            return True
        else:
            return False
    
def zhongdian(before,after):
    if jiange(before,after) == True:
        xx = round((chessPoint[before].x + chessPoint[after].x)/2,3)
        yy = round((chessPoint[before].y + chessPoint[after].y)/2,3)
        kkk = xuanzhong(xx,yy,-1,None)
    #    print("kkk=",kkk)
        if chessPoint[kkk].tf == 1:
            return True
    else:
        return False

def defen():
    defen1 = [0,0]
    for i in range(0,10):
        if 9 < chessPoint[i].num < 20:
            defen1[0] = defen1[0] + (chessPoint[i].num-10)*chessPoint[i].basic
    for i in range(54,64):
        if -1 < chessPoint[i].num < 10:
            defen1[1] = defen1[1] + (chessPoint[i].num)*(chessPoint[i].basic-10)
    return (defen1)


def chongkai(x,y):
    if 180 < x < 290 and 50 < y < 80:
        restart_program()

def is_Sameset(a=[],b=[]):
    a.sort()
    b.sort()
    if a==b:
        return True
    else:
        return False

def lunliu(k):
    pygame.draw.rect(windows1, (255,255,255), [20, 350, 230, 80], 0)

    if k%2 == 0:
        windows1.blit(cuti2.render("红方环节", True, (255,0,0)), (30, 360))
    else:
        windows1.blit(cuti2.render("蓝方环节", True, (0,0,255)), (30, 360))

dankua_xls = []


def sendd(msg):
        msg = json.dumps(msg)
        msg = msg.encode('utf-8')
        client_sock.send(msg)

xiancheng_pool = []  ##收消息池
pool_flag = [-1]
def resv():
    while True:
        data = client_sock.recv(1024)
        data = data.decode("utf-8")
        data = json.loads(data)
        xiancheng_pool.append(data)
        pool_flag[0] += 1

import itertools


def can_dankua(chess_array, goal):
    bracket2 = ["{0}%s{1}"]
    bracket3 = ["{0}%s{1}%s{2}", "({0}%s{1})%s{2}", "{0}%s({1}%s{2})"]
    bracket4 = ["{0}%s{1}%s{2}%s{3}", "({0}%s{1})%s{2}%s{3}", "({0}%s{1}%s{2})%s{3}",
                "{0}%s({1}%s{2})%s{3}", "({0}%s{1})%s({2}%s{3})"]
    bracket5 = ["{0}%s{1}%s{2}%s{3}%s{4}", "({0}%s{1})%s{2}%s{3}%s{4}", "({0}%s{1}%s{2})%s{3}%s{4}", "({0}%s{1}%s{2}%s{3})%s{4}",
                "{0}%s({1}%s{2})%s{3}%s{4}", "{0}%s({1}%s{2}%s{3})%s{4}", "({0}%s{1})%s({2}%s{3})%s{4}", "({0}%s{1})%s{2}%s({3}%s{4})", "({0}%s{1}%s{2})%s({3}%s{4})"]
    bracket6 = ["{0}%s{1}%s{2}%s{3}%s{4}%s{5}", "({0}%s{1})%s{2}%s{3}%s{4}%s{5}", "({0}%s{1}%s{2})%s{3}%s{4}%s{5}", "({0}%s{1}%s{2}%s{3})%s{4}%s{5}",
                "({0}%s{1}%s{2}%s{3}%s{4})%s{5}", "{0}%s({1}%s{2})%s{3}%s{4}%s{5}", "{0}%s({1}%s{2}%s{3})%s{4}%s{5}", "{0}%s({1}%s{2}%s{3}%s{4})%s{5}",
                "{0}%s{1}%s({2}%s{3})%s{4}%s{5}", "({0}%s{1})%s({2}%s{3})%s{4}%s{5}", "({0}%s{1}%s{2})%s({3}%s{4})%s{5}", "({0}%s{1}%s{2})%s{3}%s({4}%s{5})",
                "{0}%s({1}%s{2}%s{3})%s({4}%s{5})", "({0}%s{1}%s{2})%s({3}%s{4}%s{5})", "({0}%s{1}%s{2}%s{3})%s({4}%s{5})", ]
    length = len(chess_array)
    list_operator = ['+', '-', '*', '/']  # 运算符
    result_lists = []  # 储存全排列的列表
    if length == 2:
        bracket0 = bracket2
    if length == 3:
        bracket0 = bracket3
    if length == 4:
        bracket0 = bracket4
    if length == 5:
        bracket0 = bracket5
    if length == 6:
        bracket0 = bracket6
    for marks in itertools.product(list_operator, repeat=length-1):
        for bracket in bracket0:
            result_lists.append((bracket % marks))

    for i in set(itertools.permutations(chess_array)):
        # print(i)
        for result_list in result_lists:  # 遍历所有可能的算式（448种可能）
            final_formula = result_list.format(*i)
            # print(final_formula)
            try:
                if eval(final_formula) == goal:
                    flag = 1
                    return final_formula
                else:
                    flag = 0
            except:
                flag = 0
    if flag == 0:
        return -1

def dankua2(before,after):
    if chessPoint[before].tf == 0:
        return -1
    if zhixian(before,after) == False:
        return -1
    if xianglin(before,after):
        return -1
    if xiangdui_chessPoint[before][1] - xiangdui_chessPoint[after][1] == 0:
        return -1
    k1 = (chessPoint[before].x - chessPoint[after].x) / g2
    k2 = (chessPoint[before].y - chessPoint[after].y) / g1
    kk = abs(k2)
    if k1==0:
        kk=round(abs(k2/2))
    k_k1 = k1 / kk
    k_k2 = k2 / kk
    Numlist =[]
    Plist= []
    if zhixian(before,after):
        if kk == 1 or kk == 2 or kk == 0:
            return -1
        else:
            for i in range(1,round(kk)):
                Plist.append(xuanzhong(chessPoint[before].x-i*k_k1*g2,chessPoint[before].y-i*k_k2*g1,-1,None))
                      #   print(chessPoint[before].x+i*g2,chessPoint[before].y-i*g1)
            for j in Plist:
                     #  print(j)
                if chessPoint[j].tf != 0:
                    if 9 < chessPoint[j].num < 20:
                        Numlist.append(chessPoint[j].num-10)
                    else:
                        Numlist.append(chessPoint[j].num)
        if (len(Numlist) < 2):
            return -1
    if xiangdui_chessPoint[before][0] - xiangdui_chessPoint[after][0] == 0:
        if abs(xiangdui_chessPoint[before][1] - xiangdui_chessPoint[after][1]) != 2*(len(Numlist)+1):
            return -1
    else:
        if abs(xiangdui_chessPoint[before][0] - xiangdui_chessPoint[after][0]) != len(Numlist)+1:
            
            return -1
    if -1 < chessPoint[before].num < 10:
        if can_dankua(Numlist,chessPoint[before].num) != -1:
            print("numlist",Numlist)
            ttt = can_dankua(Numlist,chessPoint[before].num)
            return ttt
    if 9 < chessPoint[before].num < 20:
        
        if can_dankua(Numlist,chessPoint[before].num - 10) != -1:
            print("红方numlist",Numlist)
            ttt = can_dankua(Numlist,chessPoint[before].num-10)
            return ttt
    return -1
##9.19 11:30:问题：单跨后点选择以及运算量，返回值的规整

qipan = copy.deepcopy(chessPoint)

def xuanzhong2(x,y,k,sd):
    if k < 0:
        for i in range(0,64):
            xx = qipan[i].x
            yy = qipan[i].y
            quyu = pygame.Rect(xx-2*r,yy-2*r,4*r,4*r)
            if quyu.collidepoint(x,y):
                return(int(i))
    elif k >= 0 and sd == 1:
        for i in range(0,64):
            if k%2 != 0:
                xx = qipan[i].x
                yy = qipan[i].y
                quyu = pygame.Rect(xx-2*r,yy-2*r,4*r,4*r)
                if -1 < qipan[i].num <10 or qipan[i].tf == 0:
                    if quyu.collidepoint(x,y):
                        return(int(i))
    elif k >= 0 and sd == 0:
        for i in range(0,64):
            if k%2 == 0:
                xx = qipan[i].x
                yy = qipan[i].y
                quyu = pygame.Rect(xx-2*r,yy-2*r,4*r,4*r)
                if 9 < qipan[i].num < 20 or qipan[i].tf == 0:
                    if quyu.collidepoint(x,y):
                        return(int(i))     
    return(None)


def can_move2(before,after):
    if xianglin2(before,after) and qipan[before].tf > 0 and qipan[after].tf == 0:
        return True
    return False


def can_jump2(before,after):
    if zhongdian2(before,after) and qipan[before].tf>0 and qipan[after].tf==0:
        return True
    return False


def xianglin2(before,after):#判断是否相邻
    xx = abs(qipan[before].x - qipan[after].x)
    yy = abs(qipan[before].y - qipan[after].y)
    xx1 = int(xx)
    yy1 = int(yy)
    if(xx1 == int(g2) and yy1 ==int(g1)):
        return True
    elif(xx1 == 0 and yy1 == int(2*g1)):
        return True
    else:
        return False

def zhixian2(before,after):#判断是否在一条线上
    if round(abs(qipan[before].x-qipan[after].x)/g2,1) == round(abs(qipan[before].y-qipan[after].y)/g1,1):
        return True
    elif(qipan[before].x == qipan[after].x):
        return True
    else:
        return False  

def jiange2(before,after):
    if zhixian2(before,after) == True:
        if int(abs(qipan[before].x-qipan[after].x)) == int(2*g2) and int(abs(qipan[before].y-qipan[after].y)) == int(2*g1):
            return True
        elif int(abs(qipan[before].x-qipan[after].x)) == 0 and int(abs(qipan[before].y-qipan[after].y)) == int(4*g1):
            return True
        else:
            return False
    
def zhongdian2(before,after):
    if jiange2(before,after) == True:
        xx = round((qipan[before].x + qipan[after].x)/2,3)
        yy = round((qipan[before].y + qipan[after].y)/2,3)
        kkk = xuanzhong2(xx,yy,-1,None)
    #    print("kkk=",kkk)
        if qipan[kkk].tf == 1:
            return True
    else:
        return False


def dankua4(before,after):
    if qipan[before].tf == 0:
        return -1
    if zhixian2(before,after) == False:
        return -1
    if xianglin2(before,after):
        return -1
    if xiangdui_chessPoint[before][1] - xiangdui_chessPoint[after][1] == 0:
        return -1
    k1 = (qipan[before].x - qipan[after].x) / g2
    k2 = (qipan[before].y - qipan[after].y) / g1
    kk = abs(k2)
    if k1==0:
        kk=round(abs(k2/2))
    k_k1 = k1 / kk
    k_k2 = k2 / kk
    Numlist =[]
    Plist= []
    if zhixian2(before,after):
        if kk == 1 or kk == 2 or kk == 0:
            return -1
        else:
            for i in range(1,round(kk)):
                Plist.append(xuanzhong2(qipan[before].x-i*k_k1*g2,qipan[before].y-i*k_k2*g1,-1,None))
                      #   print(qipan[before].x+i*g2,qipan[before].y-i*g1)
            for j in Plist:
                     #  print(j)
                if qipan[j].tf != 0:
                    if 9 < qipan[j].num < 20:
                        Numlist.append(qipan[j].num-10)
                    else:
                        Numlist.append(qipan[j].num)
        if (len(Numlist) < 2):
            return -1
    if xiangdui_chessPoint[before][0] - xiangdui_chessPoint[after][0] == 0:
        if abs(xiangdui_chessPoint[before][1] - xiangdui_chessPoint[after][1]) != 2*(len(Numlist)+1):
            return -1
    else:
        if abs(xiangdui_chessPoint[before][0] - xiangdui_chessPoint[after][0]) != len(Numlist)+1:
            
            return -1
    if -1 < qipan[before].num < 10:
        if can_dankua(Numlist,qipan[before].num) != -1:
            print("numlist",Numlist)
            ttt = can_dankua(Numlist,qipan[before].num)
            return ttt
    if 9 < qipan[before].num < 20:
        
        if can_dankua(Numlist,qipan[before].num - 10) != -1:
            print("红方numlist",Numlist)
            ttt = can_dankua(Numlist,qipan[before].num-10)
            return ttt
    return -1

def yidong2(before,after):
    qipan[before].tf = 0
    qipan[after].tf = 1
    qipan[after].num = qipan[before].num
    qipan[before].num = -1

depth = 3

def judge(sd8):
    global depth
    judgenum = 0
    red_score = 0
    blue_score = 0
    for i in range(0,64):
        if qipan[i].num == 0:
            red_score += (0+1)*(14-xiangdui_chessPoint[i][0])
            if (0+1)*(14-xiangdui_chessPoint[i][0]) > judgenum:
                judgenum = (0+1)*(14-xiangdui_chessPoint[i][0])
                cur = i
                
        elif qipan[i].num == 1:
            red_score += (1+1)*(8-xiangdui_chessPoint[i][0])
            if (1+1)*(8-xiangdui_chessPoint[i][0]) > judgenum:
                judgenum = (1+1)*(8-xiangdui_chessPoint[i][0])
                cur = i

        elif qipan[i].num == 2:
            red_score += (2+1)*(8-xiangdui_chessPoint[i][0])
            if (2+1)*(8-xiangdui_chessPoint[i][0]) > judgenum:
                judgenum = (2+1)*(8-xiangdui_chessPoint[i][0])
                cur = i

        elif qipan[i].num == 3:
            red_score += (3+1)*(8-xiangdui_chessPoint[i][0])
            if (3+1)*(8-xiangdui_chessPoint[i][0]) > judgenum:
                judgenum = (3+1)*(8-xiangdui_chessPoint[i][0])
                cur = i

        elif qipan[i].num == 4:
            red_score += (4+1)*(8-xiangdui_chessPoint[i][0])
            if (4+1)*(8-xiangdui_chessPoint[i][0]) > judgenum:
                judgenum = (4+1)*(8-xiangdui_chessPoint[i][0])
                cur = i

        elif qipan[i].num == 5:
            red_score += (5+1)*(10-xiangdui_chessPoint[i][0])
            if (5+1)*(10-xiangdui_chessPoint[i][0]) > judgenum:
                judgenum = (5+1)*(10-xiangdui_chessPoint[i][0])
                cur = i

        elif qipan[i].num == 6:
            red_score += (6+1)*(10-xiangdui_chessPoint[i][0])
            if (6+1)*(10-xiangdui_chessPoint[i][0]) > judgenum:
                judgenum = (6+1)*(10-xiangdui_chessPoint[i][0])
                cur = i

        elif qipan[i].num == 7:
            red_score += (7+1)*(10-xiangdui_chessPoint[i][0])
            if (7+1)*(10-xiangdui_chessPoint[i][0]) > judgenum:
                judgenum = (7+1)*(10-xiangdui_chessPoint[i][0])
                cur = i

        elif qipan[i].num == 8:
            red_score += (8+1)*(12-xiangdui_chessPoint[i][0])
            if (8+1)*(12-xiangdui_chessPoint[i][0]) > judgenum:
                judgenum = (8+1)*(12-xiangdui_chessPoint[i][0])
                cur = i

        elif qipan[i].num == 9:
            red_score += (9+1)*(12-xiangdui_chessPoint[i][0])
            if (9+1)*(12-xiangdui_chessPoint[i][0]) > judgenum:
                judgenum = (9+1)*(12-xiangdui_chessPoint[i][0])
                cur = i


        if qipan[i].num == 10:
            blue_score -= (0+1)*(xiangdui_chessPoint[i][0]-0)
        elif qipan[i].num == 11:
            blue_score -= (1+1)*(xiangdui_chessPoint[i][0]-3)
        elif qipan[i].num == 12:
            blue_score -= (2+1)*(xiangdui_chessPoint[i][0]-3)
        elif qipan[i].num == 13:
            blue_score -= (3+1)*(xiangdui_chessPoint[i][0]-3)
        elif qipan[i].num == 14:
            blue_score -= (4+1)*(xiangdui_chessPoint[i][0]-3)
        elif qipan[i].num == 15:
            blue_score -= (5+1)*(xiangdui_chessPoint[i][0]-2)
        elif qipan[i].num == 16:
            blue_score -= (6+1)*(xiangdui_chessPoint[i][0]-2)
        elif qipan[i].num == 17:
            blue_score -= (7+1)*(xiangdui_chessPoint[i][0]-2)
        elif qipan[i].num == 18:
            blue_score -= (8+1)*(xiangdui_chessPoint[i][0]-1)
        elif qipan[i].num == 1:
            blue_score -= (9+1)*(xiangdui_chessPoint[i][0]-1)

        if sd8 == 0:
            return red_score + blue_score
        if sd8 == 1:
            return -red_score - blue_score




    all_score = red_score + blue_score


maxest = [None,None]

def search(depth,sd):
    best = -99999
    global qipan
    global maxest
    if depth < 1:
        return judge(sd),maxest
    if sd == 0:
        print("我是红方")
        for i in range(0,64):
            for j in range(0,64):
                if can_move2(i,j) == True or can_jump2(i,j) == True or dankua4(i,j) != -1:
                    yidong2(i,j)
                    value = -search(depth-1)[0]
                    yidong(j,i)
                    if value > best:
                        best = value
                        maxest[0] = i
                        maxest[1] = j
    return best,maxest



def Aigo(sd):
    longest = [None,None," "]
    
    mx = -1
    if sd == 0:
        print("我是红方")
        for i in range(0,64):
            for j in range(0,64):
                if (can_move(i,j) == True or can_jump(i,j) == True or dankua2(i,j) != -1) and xiangdui_chessPoint[i][0] > 4 and 9 < chessPoint[i].num < 20 and chessPoint[j].num == -1:
                    if xiangdui_chessPoint[i][0] - xiangdui_chessPoint[j][0] > mx:
                        longest[0] = i
                        longest[1] = j
                        mx = xiangdui_chessPoint[i][0] - xiangdui_chessPoint[j][0]
                        print (mx)
                        print("longlong",longest)
                        if dankua2(i,j) != -1:
                            print("单跨表达式",dankua2(i,j))
                            longest[2] = dankua2(i,j)
                    if xiangdui_chessPoint[i][0] - xiangdui_chessPoint[j][0] == mx:
                        longest[0] = i
                        longest[1] = j
                        mx = xiangdui_chessPoint[i][0] - xiangdui_chessPoint[j][0]
                        print (mx)
                        print("longlong",longest)
                        if dankua2(i,j) != -1:
                            print("单跨表达式",dankua2(i,j))
                            longest[2] = dankua2(i,j)

                    if mx == 0:
                        print("进入关键点")
                        if xiangdui_chessPoint[i][0] > 4:
                            longest[0] = i
                        if xiangdui_chessPoint[i][0] > 4:
                            if chessPoint[j].tf == -1:
                                if xiangdui_chessPoint[j][1] > xiangdui_chessPoint[i][1]:
                                    longest[1] = xiangdui_suoyin_chessPoint[(xiangdui_chessPoint[i][0],xiangdui_chessPoint[i][1]+1)]
                                if xiangdui_chessPoint[j][1] < xiangdui_chessPoint[i][1]:
                                    longest[1] = xiangdui_suoyin_chessPoint[(xiangdui_chessPoint[i][0],xiangdui_chessPoint[i][1]-1)]
                        

    if sd == 1:
        print("我是蓝方")
        for i in range(0,64):
            for j in range(0,64):
                if (can_move(i,j) == True or can_jump(i,j) == True or dankua2(i,j) != -1) and xiangdui_chessPoint[i][0] < 11 and -1 < chessPoint[i].num < 10 and chessPoint[j].num == -1:
                    if xiangdui_chessPoint[j][0] - xiangdui_chessPoint[i][0] > mx:
                        longest[0] = i
                        longest[1] = j
                        mx = xiangdui_chessPoint[j][0] - xiangdui_chessPoint[i][0]
                        if dankua2(i,j) != -1:
                            longest[2] = dankua2(i,j)
                    if xiangdui_chessPoint[i][0] - xiangdui_chessPoint[j][0] == mx:
                        longest[0] = i
                        longest[1] = j
                        mx = xiangdui_chessPoint[j][0] - xiangdui_chessPoint[i][0]
                        if dankua2(i,j) != -1:
                            longest[2] = dankua2(i,j)
                    if mx == 0:
                        print("进入关键点")
                        if xiangdui_chessPoint[i][0] > 4:
                            longest[0] = i
                        if xiangdui_chessPoint[i][0] > 4:
                            if chessPoint[j].tf == -1:
                                if xiangdui_chessPoint[j][1] > xiangdui_chessPoint[i][1]:
                                    longest[1] = xiangdui_suoyin_chessPoint[(xiangdui_chessPoint[i][0],xiangdui_chessPoint[i][1]+1)]
                                if xiangdui_chessPoint[j][1] < xiangdui_chessPoint[i][1]:
                                    longest[1] = xiangdui_suoyin_chessPoint[(xiangdui_chessPoint[i][0],xiangdui_chessPoint[i][1]-1)]
    return (longest)

                    

        
            
import time

def main():
    start_new_thread(resv,())
    wanjia1 = {"type":0,"msg":{"name":"yushulinfeng"}}
    # yidong_dir1["msg"]["src"]["x"]
    # yidong_dir1["msg"]["src"]["y"]
    sendd(wanjia1)
    yidong_dir1 = {"type": 1,"msg":{"game_id": "","side": "","num": 0,"src": {"x": 0,"y": 0},"dst":{"x": 0,"y": 0},"exp": ""}}
    jiao_ting = {"type":2,"msg":{"request": "stop","game_id":"","side":""}}
    tui_chu = {"type":2,"msg": {"request": "quit","game_id":"","side":""}}
    jie_shu = {"type":3,"side":""}
    # if 
    while True:
        make_Pan()
        make_others()
        huaqipan()
        huashuzi()
        lunliu(0)
        pygame.display.update()
        eventlist = [None,None]
        k = 0
        flag2 = 0
        flag3 = 0
        while True:
            if len(xiancheng_pool) >= 1:
                if xiancheng_pool[0]["side"] == 1:
                    sd2 = 1
                    flag2 = 1

                if xiancheng_pool[0]["side"] == 0:
                    sd2 = 0
                    flag2 = 1
                yidong_dir1["msg"]['side'] = xiancheng_pool[0]["side"]
                jiao_ting["msg"]['side'] = xiancheng_pool[0]["side"]
                tui_chu["msg"]['side'] = xiancheng_pool[0]["side"]
                jie_shu["side"] = xiancheng_pool[0]["side"]
                if xiancheng_pool[0]["side"] == 0:
                      
                    windows1.blit(cuti2.render("我为红方", True, (0,0,0)), (30,270))
                elif xiancheng_pool[0]["side"] == 1:
             
                    windows1.blit(cuti2.render("我为蓝方", True, (0,0,0)), (30,270))
            if len(xiancheng_pool) > 1 and (len(xiancheng_pool[-1]) == 4 or len(xiancheng_pool[-1]) == 3):
                if xiancheng_pool[-1]["status"] == 3:
                    if(xiancheng_pool[-1]["side"] != yidong_dir1["msg"]["side"]):
                        root3=Tk()
                        Label(root3,text='对方超时').pack()
                        root3.geometry("%dx%d+%d+%d" % (200, 100, (WINDOWWIDTH +50) / 2, (WINDOWHEIGHT +50) / 2))
                        root3.mainloop()
                        pygame.quit()
                        sys.exit()
                    else:
                        root3=Tk()
                        Label(root3,text='您已超时').pack()
                        root3.geometry("%dx%d+%d+%d" % (200, 100, (WINDOWWIDTH +50) / 2, (WINDOWHEIGHT +50) / 2))
                        root3.mainloop()
                        pygame.quit()
                        sys.exit()
                if xiancheng_pool[-1]["request"] == "stop":
                    jiaoting(k)
                    sendd(jie_shu)
                elif xiancheng_pool[-1]["request"] == "quit":
                    root3=Tk()
                    Label(root3,text='对方认输').pack()
                    root3.geometry("%dx%d+%d+%d" % (200, 100, (WINDOWWIDTH +50) / 2, (WINDOWHEIGHT +50) / 2))
                    root3.mainloop()
                    pygame.quit()
                    sys.exit()
            else:
                if (len(xiancheng_pool) == 1):
                    x = xiancheng_pool[0]
                    resv_msg = x
                    yidong_dir1["msg"]["game_id"] = resv_msg["game_id"]
                    jiao_ting["msg"]["game_id"] = resv_msg["game_id"]
                    tui_chu["msg"]["game_id"] = resv_msg["game_id"]

                make_defen()
                lunliu(k)
                TF = False
                TTFF = False
                for event in pygame.event.get():
                    if event.type == pygame.QUIT:
                        pygame.quit()
                        sys.exit()


                if ( flag2 % 2 != 0 and pool_flag[0] % 2 != 0 and len(xiancheng_pool) > 1 and len(xiancheng_pool[-1])==6) :
                    if len(xiancheng_pool) > 1:
                        print(xiancheng_pool)
                        eventlist[0] = xiangdui_suoyin_chessPoint[(xiancheng_pool[-1]["src"]["x"],xiancheng_pool[-1]["src"]["y"])]
                        eventlist[1] = xiangdui_suoyin_chessPoint[(xiancheng_pool[-1]["dst"]["x"],xiancheng_pool[-1]["dst"]["y"])]
                        TTFF = True
                

                if  (flag2 % 2 != 0 and flag3 % 2 == 0  and sd2 == 0) or (flag2 % 2 != 0 and flag3 % 2 == 1 and sd2 == 1):
                    print("调试进入")
                    if len(xiancheng_pool) > 0:
                        tttt = Aigo(xiancheng_pool[0]["side"])

                        eventlist[0] = tttt[0]
                        eventlist[1] = tttt[1]
                    if (eventlist[0] != None and eventlist[1] != None and  eventlist[1] != eventlist[0] and can_move(eventlist[0],eventlist[1]) == True):
                        goal_p1 = xiangdui_chessPoint[eventlist[0]]
                        yidong_dir1["msg"]["src"]["x"] = goal_p1[0]
                        yidong_dir1["msg"]["src"]["y"] = goal_p1[1]
                        goal_p2 = xiangdui_chessPoint[eventlist[1]]
                        yidong_dir1["msg"]["dst"]["x"] = goal_p2[0]
                        yidong_dir1["msg"]["dst"]["y"] = goal_p2[1]
                        if -1 < chessPoint[eventlist[0]].num < 10:
                            yidong_dir1["msg"]["num"]  = chessPoint[eventlist[0]].num
                        elif 9 < chessPoint[eventlist[0]].num < 20:
                            yidong_dir1["msg"]["num"]  = chessPoint[eventlist[0]].num - 10
                        yidong(eventlist[0],eventlist[1])
                        huaqipan()
                        huashuzi()
                        k+=1
                        print("我移动")  
                        print(yidong_dir1)
                        sendd(yidong_dir1)
                        
                        # print("收到消息",resv())
                     

                    elif (eventlist[0] != None and eventlist[1] != None and  eventlist[1] != eventlist[0] and can_jump(eventlist[0],eventlist[1]) == True ):
                        goal_p1 = xiangdui_chessPoint[eventlist[0]]
                        yidong_dir1["msg"]["src"]["x"] = goal_p1[0]
                        yidong_dir1["msg"]["src"]["y"] = goal_p1[1]
                        goal_p2 = xiangdui_chessPoint[eventlist[1]]
                        yidong_dir1["msg"]["dst"]["x"] = goal_p2[0]
                        yidong_dir1["msg"]["dst"]["y"] = goal_p2[1]
                        if -1 < chessPoint[eventlist[0]].num < 10:
                            yidong_dir1["msg"]["num"]  = chessPoint[eventlist[0]].num
                        elif 9 < chessPoint[eventlist[0]].num < 20:
                            yidong_dir1["msg"]["num"]  = chessPoint[eventlist[0]].num - 10
                        print("我跳动")
                        print(yidong_dir1)  
                        sendd(yidong_dir1)
                        yidong(eventlist[0],eventlist[1])
                        huaqipan()
                        huashuzi()
                        k+=1
                  

                    elif (eventlist[0] != None and eventlist[1] != None and eventlist[1] != eventlist[0] and dankua2(eventlist[0],eventlist[1]) != -1 ):
   
                        goal_p1 = xiangdui_chessPoint[eventlist[0]]
                        yidong_dir1["msg"]["src"]["x"] = goal_p1[0]
                        yidong_dir1["msg"]["src"]["y"] = goal_p1[1]
                        goal_p2 = xiangdui_chessPoint[eventlist[1]]
                        yidong_dir1["msg"]["dst"]["x"] = goal_p2[0]
                        yidong_dir1["msg"]["dst"]["y"] = goal_p2[1]
                        yidong_dir1["msg"]["exp"] = "  "
                        if -1 < chessPoint[eventlist[0]].num < 10:
                            yidong_dir1["msg"]["num"]  = chessPoint[eventlist[0]].num
                        elif 9 < chessPoint[eventlist[0]].num < 20:
                            yidong_dir1["msg"]["num"]  = chessPoint[eventlist[0]].num - 10
                        print("我单跨动")
                        print(yidong_dir1)  
                        sendd(yidong_dir1)
                        yidong(eventlist[0],eventlist[1])
                        huaqipan()
                        huashuzi()
                        
                        k+=1
                        
                elif TTFF == True:

                    if (eventlist[0] != None and eventlist[1] != None and  eventlist[1] != eventlist[0] and can_move(eventlist[0],eventlist[1]) == True):
                    #  
                    
                        yidong(eventlist[0],eventlist[1])
                        huaqipan()
                        huashuzi()
                        k+=1
                        pool_flag[0] += 1
                        sd2 += 1
                        flag3 += 1
                        print ("移")
                        print("对面移动")  
                        print(yidong_dir1)
                    
                       

                    elif (eventlist[0] != None and eventlist[1] != None and  eventlist[1] != eventlist[0] and can_jump(eventlist[0],eventlist[1]) == True ):
                
                        print("对面跳动")
                        print(yidong_dir1)  
                    
                        yidong(eventlist[0],eventlist[1])
                        huaqipan()
                        huashuzi()
                        sd2 += 1
                        k+=1
                        pool_flag[0] += 1
                        flag3 += 1
        
                    elif (eventlist[0] != None and eventlist[1] != None and eventlist[1] != eventlist[0]):
                        print("玩家2单跨动")
                        print(yidong_dir1)  
                
                        yidong(eventlist[0],eventlist[1])
                        huaqipan()
                        huashuzi()
                        sd2 += 1
                        k+=1
                        pool_flag[0] += 1
                        flag3 += 1
                        print ("单跨")
  
                pygame.display.update()
                

        
if __name__=='__main__':
    main()


#9.22 12：04 ：先手存在bug，叫停功能有问题